package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.LayoutEntity;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.service.ILayoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class LayoutController {
    @Autowired
    private ILayoutService iLayoutService;
    @RequestMapping("/admin/layout/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, LayoutEntity layoutEntity, HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        layoutEntity.setStatus(Tool.stringToInt(status));
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        layoutEntity.setPageEntity(pageEntity);
        List<LayoutEntity> list = iLayoutService.findAll(layoutEntity);
        int count = iLayoutService.getCount(layoutEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/layout/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp, LayoutEntity layoutEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LayoutEntity model = iLayoutService.findById(layoutEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/layout/add")
    @ResponseBody
    public void add(HttpServletResponse resp, LayoutEntity layoutEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        layoutEntity.setStatus(Tool.stringToInt((String)req.getAttribute("status")));
        layoutEntity.setTitle((String) req.getAttribute("title"));
        layoutEntity.setImg((String) req.getAttribute("img"));
        layoutEntity.setContent((String) req.getAttribute("content"));
        layoutEntity.setSecondTitle((String) req.getAttribute("secondTitle"));
        layoutEntity.setSecondContent((String) req.getAttribute("secondContent"));
        layoutEntity.setThirdTitle((String) req.getAttribute("thirdTitle"));
        String result = iLayoutService.add(layoutEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/layout/update")
    @ResponseBody
    public void update(HttpServletResponse resp, LayoutEntity layoutEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        layoutEntity.setStatus(Tool.stringToInt((String)req.getAttribute("status")));
        layoutEntity.setId(Tool.stringToInt((String)req.getAttribute("id")));
        layoutEntity.setTitle((String) req.getAttribute("title"));
        layoutEntity.setImg((String) req.getAttribute("img"));
        layoutEntity.setContent((String) req.getAttribute("content"));
        layoutEntity.setSecondTitle((String) req.getAttribute("secondTitle"));
        layoutEntity.setSecondContent((String) req.getAttribute("secondContent"));
        layoutEntity.setThirdTitle((String) req.getAttribute("thirdTitle"));
        String result = iLayoutService.update(layoutEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/layout/del")
    @ResponseBody
    public void del(HttpServletResponse resp, LayoutEntity layoutEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        int result = iLayoutService.del(layoutEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
//    @RequestMapping("/admin/layout/getLayoutModelListForHome")
//    @ResponseBody
//    public void getLayoutModelListForHome(HttpServletResponse resp, LayoutEntity layoutEntity,HttpServletRequest req) throws IOException {
//        Tool.setEncoding(req,resp);
//        List<LayoutEntity> list = iLayoutService.getLayoutModelListForHome(layoutEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("list",list);
//        resp.getWriter().println(jsonObject);
//    }

}
